<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./vue.js"></script>

    <!-- 第一步引入  vue-router.js -->
    <script src="./vue-router.js"></script>
  </head>

  <body>
    <div id="app">
      <h1>我是跟组件</h1>
      <!--  router-link 就是a 标签   to  就是 href   to 后面跟的是path 定义的路径-->

      <!-- 使用 router-link 组件来导航. -->
      <!-- 通过传入 `to` 属性指定链接. -->
      <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->

      <router-link to="/books"> books</router-link>
      <router-link to="/news"> news </router-link>

      <router-link to="/love"> love </router-link>
      <hr />

      <!--  渲染占位符   对应的路由在哪里显示-->
      <!-- 路由出口 -->
      <!-- 路由匹配到的组件将渲染在这里 -->
      <router-view></router-view>
    </div>
    <script>
      // 1. 定义 (路由) 组件。
      const Books = {
        template: "<h1>  books 组件</h1>",
      };
      const News = {
        template: "<h1>  News 组件</h1>",
      };
      const Love = {
        template: `<div>
                
                 <h1>  Love 组件</h1>
                <router-link to='/love/sing'>love/sing</router-link>
                <router-link to='/love/dancing'> love/dancing </router-link>
                <router-view></router-view>

                </div>`,
      };

      const Sing = {
        template: "<h1>  Sing 组件</h1>",
      };

      const Dancing = {
        template: "<h1>  Dancing 组件</h1>",
      };

      //    第二步 创建实例对象
      let myRouter = new VueRouter({
        // 4.1  配置路由
        routes: [
          //    {  第一个参数 path, 第二个参数   组件 component }
          { path: "/books", component: Books },
          { path: "/news", component: News },
          // 路由嵌套
          {
            path: "/love",
            component: Love,

            children: [
              {
                path: "sing",
                component: Sing,
              },
              {
                path: "dancing",
                component: Dancing,
              },
            ],
          },
        ],
      });
      new Vue({
        el: "#app",
        data: {},
        //  第三步 挂载路由到vue 实例对象身上
        router: myRouter,
      });
    </script>
  </body>
</html>
